package com.springcloud.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**https://www.cnblogs.com/babycomeon/p/11173609.html
 * @description:  限流的策略
 * @see:com.springcloud.gateway.config
 * @author:xuan
 * @createTime:2020/12/19 18:27
 * @version:1.0
 */
@Configuration
public class Config {
    @Bean
    KeyResolver userKeyResolver() {
        // 根据请求参数中的 user 字段来限流
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
    }

    /**
     * 设置根据请求 IP 地址来限流，设置如下:
     * @return
     */
    @Bean
    public KeyResolver ipKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }
}